package com.vodone.v1pin.common.bussiness
{
	
	import com.vodone.v1pin.core.model.MainModelLocator;
	import com.vodone.v1pin.core.vo.ConfigVO;
	import com.vodone.v1pin.navigator.view.vo.NavVO;
	
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;

	public class ConfigManager extends EventDispatcher
	{
		private static var _instance:ConfigManager;
		
		public function ConfigManager()
		{
			if(null != _instance){
				return;
			}
		}
		
		public static function getInstance():ConfigManager
		{
			if(_instance == null){
				_instance = new ConfigManager();
			}
			return _instance;
		}
		
		
		public function getNavigator():void
		{
			var request:URLRequest = new URLRequest("config/navigator.xml?params="+MainModelLocator.getInstance().fix);
			var urlLoader:URLLoader = new URLLoader();
			urlLoader.addEventListener(Event.COMPLETE,loadNavConfigCompleteHandler);
			urlLoader.addEventListener(ErrorEvent.ERROR,loadConfigErrorHandler);
			urlLoader.addEventListener(IOErrorEvent.IO_ERROR,loadConfigErrorHandler);
			urlLoader.load(request);
		}
		
		public function getAddressConfig():void
		{
			var request1:URLRequest = new URLRequest("config/config.xml?params="+MainModelLocator.getInstance().fix);
			var urlLoader1:URLLoader = new URLLoader();
			urlLoader1.addEventListener(Event.COMPLETE,loadAddressConfigCompleteHandler);
			urlLoader1.addEventListener(ErrorEvent.ERROR,loadConfigErrorHandler);
			urlLoader1.addEventListener(IOErrorEvent.IO_ERROR,loadConfigErrorHandler);
			urlLoader1.load(request1);
		}
		
		public function loadAddressConfigCompleteHandler(e:Event):void
		{
			MainModelLocator.getInstance().config = new ConfigVO();
			var loader:URLLoader = e.target as URLLoader;
			var xml:XML = XML(loader.data);
			
			var len:uint = xml.item.length();
			for(var i:uint=0;i<len;i++)
			{
				var child:XML = xml.item[i];
				MainModelLocator.getInstance().config.infoUrl = child.getInfoUrl;
				MainModelLocator.getInstance().config.v1Url = child.getV1Url;
				MainModelLocator.getInstance().config.imageUrl = child.getPicUrl;
				MainModelLocator.getInstance().config.navURL = child.navURL;
			}
			dispatchResult( "config" );	
		}
		
		private function loadNavConfigCompleteHandler(e:Event):void
		{
			var loader:URLLoader = e.target as URLLoader;
			var navXML:XML = XML(loader.data);
			
			var nav1len:uint = navXML.nav2.length();
			
			var navList:Array = [];
			
			for(var i:uint=0;i<nav1len;i++){
				var nav1xml:XML = navXML.nav2[i];
				var nav:NavVO = new NavVO();
				nav.currentId = nav1xml.@id;
				nav.buttonImage = nav1xml.@nomalPic;
				MainModelLocator.getInstance().navgatorButtonDict[nav.currentId] = nav;
			}
			/*for(var i:uint=0;i<nav1len;i++){
				var nav1xml:XML = navXML.nav1[i];
				var nav1:NavVO = new NavVO();
				nav1.level = 1;
				nav1.index = i;
				nav1.name = nav1xml.name;
				nav1.nextNavList = new Array();
				var nav2len:uint = nav1xml.nav2.nav2.length();
				for(var j:uint=0;j<nav2len;j++){
					var nav2xml:XML = nav1xml.nav2.nav2[j];
					var nav2:NavVO = new NavVO();
					nav2.level = 2;
					nav2.index = j;
					nav2.rowcount = nav2xml.@rowcount;
					nav2.name = nav2xml.name;
					nav2.nextNavList = new Array();
					var nav3len:uint = nav2xml.nav3.nav3.length();
					for(var z:uint=0;z<nav3len;z++){
						var nav3xml:XML = nav2xml.nav3.nav3[z];
						var nav3:NavVO = new NavVO();
						nav3.level = 3;
						nav3.index = z;
						nav3.type = NavVO.LEVEL3;
						if(z>5){
							nav3.type = "pp"
						}
						nav3.name = nav3xml.name;
						nav2.nextNavList.push(nav3);
					}
					nav1.nextNavList.push(nav2);
				}
				
				navList.push( nav1 );
			}*/
//			MainModelLocator.getInstance().navgatorList = navList;
		}
		
		
		private function dispatchResult( list:* ):void
		{
			var loadCompleteEvt:LoadConfigCompleteEvent = new LoadConfigCompleteEvent( list );
			this.dispatchEvent( loadCompleteEvt );
		}
		
		private function loadConfigErrorHandler( e:Event ):void
		{
			
		}
	}
}